package com.ch.livedatademo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders
import java.util.*

class MainActivity : AppCompatActivity() {
    private lateinit var myViewModel: MyViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val mTextView: TextView = findViewById(R.id.text_view)
        myViewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
        mTextView.text=myViewModel.getCurRentTime().toString()
        startTime()
        myViewModel.getCurRentTime().observe(this){
            Log.e("ab",it.toString())
            mTextView.text=it.toString()
        }
    }

    private fun startTime() {
      Timer().schedule(object: TimerTask() {
          override fun run() {
            myViewModel.getCurRentTime().postValue(
                myViewModel.getCurRentTime().value?.plus(1)
            )
          }

      },1000,1000)
    }
}


